package com;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESEncrypt {
    protected static String thisiv;
    // iv 必须要是一个16为的字符串
    public static void setIv(String iv) {
    	thisiv =  iv;
    }
    /** 
     * 加密 
     * @method encrypt 
     * @param strIn   需要加密的内容 
     * @param strKey  加密密码 
     * @return 
     * @throws  
     * @since v1.0 
     */  
    public static String encrypt(String strKey, String strIn) throws Exception {
        SecretKeySpec skeySpec = getKey(strKey);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(thisiv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(strIn.getBytes());

        return new BASE64Encoder().encode(encrypted);
    }

    /** 
     * 解密 
     * @method decrypt 
     * @param strIn   待解密内容 
     * @param strKey  解密密钥 
     * @return 
     * @throws  
     * @since v1.0 
     */  
    public static String decrypt(String strKey, String strIn) throws Exception {
        SecretKeySpec skeySpec = getKey(strKey);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(thisiv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] encrypted1 = new BASE64Decoder().decodeBuffer(strIn);

        byte[] original = cipher.doFinal(encrypted1);
        String originalString = new String(original);
        return originalString;
    }
    /** 
     * 将二进制转换成16进制 
     * @method parseByte2HexStr 
     * @param buf 
     * @return 
     * @throws  
     * @since v1.0 
     */  
    private static String parseByte2HexStr(byte buf[]){  
        StringBuffer sb = new StringBuffer();  
        for(int i = 0; i < buf.length; i++){  
            String hex = Integer.toHexString(buf[i] & 0xFF);  
            if (hex.length() == 1) {  
                hex = '0' + hex;  
            }  
            sb.append(hex.toUpperCase());  
        }  
        return sb.toString();  
    }  
    /** 
     * 转换密钥<br> 
     *  
     * @param strKey 
     * @return 
     * @throws Exception 
     */  
    private static SecretKeySpec getKey(String strKey) throws Exception {
        byte[] arrBTmp = strKey.getBytes();
        byte[] arrB = new byte[16]; // 创建一个空的16位字节数组（默认值为0）

        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }

        SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");
        
        return skeySpec;
    }

    public static void main(String[] args) throws Exception {
        String Code = "你好你好";
//        String key = "1q2w3e4r*🐶";
        String key = "1q2w3e4rei1q2w3e4rei";
        String codE, codePhp, codeObjc;
        AESEncrypt.setIv("abc2030405060708");
        codE = AESEncrypt.encrypt(key, Code);
        codePhp = "u6qq89cZALmD+1PX6dXRgA==";
        codeObjc = "MSVbpcJlzq/HqcpSoHA4SA==";
        
        
        System.out.println("原文：" + Code);
        System.out.println("密钥：" + key);
        System.out.println("密文：" + codE);
        System.out.println("解密java：" + AESEncrypt.decrypt(key, codE));
//        System.out.println("解密php：" + AESEncrypt.decrypt(key, codePhp));
//        System.out.println("解密objc：" + AESEncrypt.decrypt(key, codeObjc));
//        String key2 = "1q2w3e4rei9273hflsiejfhdkwieu493";
//        String con2 = "5CxL+rE6Gh/zKPcffYExDg==";
//        System.out.println("解密objc：" + AESEncrypt.decrypt(key, con2));

//        String res = webview.postUrl("http://127.0.0.1:80","data=dhfakjsdfhksdhf+dfasjkdhf+adsfkhasld");
//        System.out.println("webview文：" + res);
        
    }
}

